package com.haochang.chunkvod.controller.activity.cash;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseEditText;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.cash.NoRowGoodsAdapter;
import com.haochang.chunkvod.model.cash.CashData;
import com.haochang.chunkvod.model.cash.GoodInfo;
import com.haochang.chunkvod.view.vod.dialog.DialogItemInfo;
import com.haochang.chunkvod.view.vod.dialog.KeyBoardDialog;
import com.haochang.chunkvod.view.vod.dialog.SingleSelectionDialog;
import com.haochang.chunkvod.view.vod.dialog.VodCustomDialog;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * @author qintao
 *         created at 2016/11/8 12:46
 *         买单
 */

public class BuyOrderActivity extends BaseActivity implements KeyBoardDialog.KeyBoardOnSureListener
{
	private TitleView titleView;
	private BaseTextView tv_consumeType;
	private BaseTextView tv_startTime;
	private BaseTextView tv_buyOrderType;
	private RelativeLayout buy_order_type_div, buy_order_time_div;
	private BaseTextView tv_consume;
	private BaseTextView tv_pend;
	private BaseTextView tv_payForAnother;
	private BaseTextView tv_prePay;
	private BaseTextView tv_coupons;
	private BaseTextView tv_deduct;
	private BaseListView listView;
	private NoRowGoodsAdapter noRowGoodsAdapter;
	private LinearLayout pay_div, time_div;
	private RelativeLayout bottom_tip_div;
	private RelativeLayout pend_div, couponLayout_div, payForAnother_div, prePay_div;
	private BaseTextView tv_count_buyOrder;
	private BaseTextView tv_count_refund;
	private BaseEditText et_toHour, et_toMinute;
	private Button btn_buyOrder;
	private String roomNum;
	private List<DialogItemInfo> butOrderTypeList = new ArrayList<DialogItemInfo>();//买单方式列表
	private List<GoodInfo> goodInfoList = new ArrayList<GoodInfo>();
	private boolean isTableBuyOrder = false;
	private int currentViewId = 0;
	private CashData cashData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{
		roomNum = getIntent().getStringExtra(IntentKey.ROOM_NUM);
		isTableBuyOrder = getIntent().getBooleanExtra(IntentKey.TABLE_BUY_ORDER, false);
	}

	@Override
	public void initData()
	{
		GoodInfo goodInfo = new GoodInfo();
		goodInfo.setGoodsName("水电费");
		goodInfo.setUnit("分");
		goodInfo.setPrice(2000);
		goodInfo.setOrderNum(2);
		goodInfoList.add(goodInfo);
		goodInfoList.add(goodInfo);
		goodInfoList.add(goodInfo);
		DialogItemInfo timeInfo1 = new DialogItemInfo("preType", "预买", true);
		DialogItemInfo timeInfo2 = new DialogItemInfo("currType", "现买", false);
		butOrderTypeList.add(timeInfo1);
		butOrderTypeList.add(timeInfo2);
		cashData = new CashData(BuyOrderActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_buy_order);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(String.format("买单(%s)", roomNum)).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(BuyOrderActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		listView = (BaseListView) findViewById(R.id.listView);
		View headerView = LayoutInflater.from(context).inflate(R.layout.buy_order_header, listView, false);
		listView.addHeaderView(headerView);
		couponLayout_div = (RelativeLayout) headerView.findViewById(R.id.couponLayout_div);
		couponLayout_div.setOnClickListener(onBaseClickListener);
		pend_div = (RelativeLayout) headerView.findViewById(R.id.pend_div);
		pend_div.setOnClickListener(onBaseClickListener);
		payForAnother_div = (RelativeLayout) headerView.findViewById(R.id.payForAnother_div);
		payForAnother_div.setOnClickListener(onBaseClickListener);
		prePay_div = (RelativeLayout) headerView.findViewById(R.id.prePay_div);
		prePay_div.setOnClickListener(onBaseClickListener);
		tv_consumeType = (BaseTextView) headerView.findViewById(R.id.tv_consumeType);
		tv_startTime = (BaseTextView) headerView.findViewById(R.id.tv_startTime);
		tv_consumeType.setText(Html.fromHtml("消费类型：" + "<font color=#f04e39>" + "分钟计费" + "</font>"));
		tv_startTime.setText(Html.fromHtml("进场时间：" + "<font color=#f04e39>" + "19:00" + "</font>"));
		buy_order_type_div = (RelativeLayout) headerView.findViewById(R.id.buy_order_type_div);
		buy_order_time_div = (RelativeLayout) headerView.findViewById(R.id.buy_order_time_div);
		buy_order_type_div.setOnClickListener(onBaseClickListener);
		et_toHour = (BaseEditText) findViewById(R.id.et_toHour);
		et_toMinute = (BaseEditText) findViewById(R.id.et_toMinute);
		et_toHour.setOnClickListener(onBaseClickListener);
		et_toMinute.setOnClickListener(onBaseClickListener);
		tv_buyOrderType = (BaseTextView) headerView.findViewById(R.id.tv_buyOrderType);
		tv_consume = (BaseTextView) headerView.findViewById(R.id.tv_consume);
		tv_pend = (BaseTextView) headerView.findViewById(R.id.tv_pend);
		tv_payForAnother = (BaseTextView) headerView.findViewById(R.id.tv_payForAnother);
		tv_prePay = (BaseTextView) headerView.findViewById(R.id.tv_prePay);
		tv_coupons = (BaseTextView) headerView.findViewById(R.id.tv_coupons);
		tv_deduct = (BaseTextView) headerView.findViewById(R.id.tv_deduct);
		bottom_tip_div = (RelativeLayout) headerView.findViewById(R.id.bottom_tip_div);
		pay_div = (LinearLayout) headerView.findViewById(R.id.pay_div);
		time_div = (LinearLayout) headerView.findViewById(R.id.time_div);
		noRowGoodsAdapter = new NoRowGoodsAdapter(BuyOrderActivity.this, R.layout.item_take_out_child, goodInfoList);
		listView.setAdapter(noRowGoodsAdapter);
		if (CollectionUtils.isEmpty(goodInfoList))
			bottom_tip_div.setVisibility(View.GONE);
		tv_count_buyOrder = (BaseTextView) findViewById(R.id.tv_count_buyOrder);
		tv_count_refund = (BaseTextView) findViewById(R.id.tv_count_refund);
		btn_buyOrder = (Button) findViewById(R.id.btn_buyOrder);
		btn_buyOrder.setOnClickListener(onBaseClickListener);
		if (isTableBuyOrder)
			hideView();
		requestData();
	}

	/**
	 * 桌台买单隐藏控件
	 */
	private void hideView()
	{
		tv_consumeType.setVisibility(View.GONE);
		time_div.setVisibility(View.GONE);
		pay_div.setVisibility(View.GONE);
		pend_div.removeAllViews();
	}

	/**
	 * 请求数据
	 */
	private void requestData()
	{
		cashData.requestBuyOrderInfo("", new CashData.IBuyOrderListener()
		{
			@Override
			public void onRequestSuccess(JSONObject jsonObject, List<GoodInfo> goodInfoList)
			{

			}

			@Override
			public void onRequestFail(int errno, String errMsg)
			{

			}
		});
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.couponLayout_div:
				//券
				startEnterActivityForResult(CouponsDetailsActivity.class, IntentCode.CASH_COUPON);
				break;
			case R.id.buy_order_type_div:
				new SingleSelectionDialog.Builder(BuyOrderActivity.this).setTitle(R.string.buy_order_type)
						.setSingleChoiceItems(butOrderTypeList, new SingleSelectionDialog.OnSelectListener()
						{
							@Override
							public void getValue(String code, String name)
							{
								if (!TextUtils.isEmpty(name))
								{
									tv_buyOrderType.setText(name);
									if (TextUtils.equals(name, "现买"))
										buy_order_time_div.setVisibility(View.GONE);
									else
										buy_order_time_div.setVisibility(View.VISIBLE);
								}
							}
						}).createDialog().show();
				break;
			case R.id.payForAnother_div:
				onPayForAnotherClick();
				break;
			case R.id.pend_div:
				onPendDetailsClick();
				break;
			case R.id.prePay_div:
				prePayDetailClick();
				break;
			case R.id.btn_buyOrder:
				startEnterActivity(CheckOutActivity.class);
				break;
			case R.id.et_toHour:
				currentViewId = R.id.et_toHour;
				showKeyBoardDialog();
				break;
			case R.id.et_toMinute:
				currentViewId = R.id.et_toMinute;
				showKeyBoardDialog();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 点击代付
	 */
	private void onPayForAnotherClick()
	{
		Intent intent = new Intent(BuyOrderActivity.this, PayForAnotherActivity.class);
		intent.putExtra(IntentKey.CASH_PAY_TYPE, PayForAnotherActivity.PayEnum.ANOTHER_PAY);
		startActivityForResult(intent, IntentCode.PAY_FOR_ANOTHER);
	}

	/**
	 * 点击挂单
	 */
	private void onPendDetailsClick()
	{
		Intent intent = new Intent(BuyOrderActivity.this, PendDetailsActivity.class);
		startActivity(intent);
	}

	private void prePayDetailClick()
	{
		new VodCustomDialog.Builder(this).setTitle("预付详情").showDialog(R.layout.inflate_vod_custom_dialog_advanced_pay, new VodCustomDialog.OnViewAddedListener()
		{
			@Override
			public void getView(View view)
			{
				((BaseTextView) view.findViewById(R.id.tv_cash)).setText(String.valueOf("现金:¥" + "555.00"));
				((BaseTextView) view.findViewById(R.id.tv_swiping_card)).setText(String.valueOf("刷卡:¥" + "555.00"));
				((BaseTextView) view.findViewById(R.id.tv_alipay)).setText(String.valueOf("支付宝:¥" + "555.00"));
				((BaseTextView) view.findViewById(R.id.tv_wchat_pay)).setText(String.valueOf("微信:¥" + "555.00"));
				((BaseTextView) view.findViewById(R.id.tv_membership_card)).setText(String.valueOf("会员卡:¥" + "555.00"));
			}
		});
	}

	/**
	 * 自定义键盘输入
	 */
	private void showKeyBoardDialog()
	{
		KeyBoardDialog keyBoardDialog = new KeyBoardDialog(BuyOrderActivity.this);
		keyBoardDialog.setKeyBoardOnSureListener(BuyOrderActivity.this);
		keyBoardDialog.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK)
		{
			if (requestCode == IntentCode.PAY_FOR_ANOTHER)
			{
				requestData();
			} else if (requestCode == IntentCode.CASH_COUPON)
			{
				//从券页面录入券成功后返回，刷新页面
			}
		}
	}

	@Override
	public void setValue(String value)
	{
		if (value.contains("."))
			ToastUtils.showText(BuyOrderActivity.this, R.string.error_number);
		else if (value.length() > 9)
			ToastUtils.showText(BuyOrderActivity.this, R.string.error_max_number);
		else
		{
			switch (currentViewId)
			{
			case R.id.et_toHour:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 23)
				{
					et_toHour.setText(value);
				} else
				{
					et_toHour.setText("23");
				}
				break;
			case R.id.et_toMinute:
				if (getIntValue(value) >= 0 && getIntValue(value) <= 59)
				{
					et_toMinute.setText(value.length() < 2 ? "0" + value : value);
				} else
				{
					et_toMinute.setText("59");
				}
				break;
			default:
				break;
			}
		}
	}

	private int getIntValue(String value)
	{
		if (TextUtils.isEmpty(value))
			return 0;
		else
			return Integer.parseInt(value);
	}
}
